using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Globalization;
using Instedd.Labs.Geo.Library.Kml;
using Instedd.Labs.Geo.Library.Kml.Styles;
using KmlPath = Instedd.Labs.Geo.Library.Kml.Path;
using Instedd.Labs.Geo.Library.Sms;

namespace Instedd.Labs.Geo.Library.Sms
{
    public class KmlManager
    {
        public KmlManager()
        {
            this.kmlWriter = new KmlWriter();
        }

        private KmlWriter kmlWriter;

        public KmlWriter KmlWriter
        {
            get { return kmlWriter; }
        }

        private Placemark CreatePlacemark(string name, LatLong location, IEnumerable<SmsExtension> messages, KmlFactorySettings settings)
        {
            SmsExtension lastUserMessage = null;

            string placemarkDescription = BubbleDescriptionGenerator.GetDescription(name, location, messages, settings, out lastUserMessage);
           
            Placemark placemark = new Placemark(name, placemarkDescription, location, lastUserMessage.Sms.Time);
            placemark.StyleUrl = GetPlacemarkStyleBasedOnReceiveTime(lastUserMessage.Sms.Time);

            return placemark;
        }

        private string GetPlacemarkStyleBasedOnReceiveTime(DateTime receiveMessageTime)
        {
            TimeSpan diff = DateTime.Now.Subtract(receiveMessageTime);
            if (diff.TotalMinutes < 15d)
            {
                return StylesUtil.MessageMinutesAgoCategory1Style;
            }
            else if (diff.TotalMinutes < 30d)
            {
                return StylesUtil.MessageMinutesAgoCategory2Style;
            }
            else if (diff.TotalMinutes < 60d)
            {
                return StylesUtil.MessageMinutesAgoCategory3Style;
            }
            else
            {
                return StylesUtil.MessageMinutesAgoCategory4Style;
            }
        }

        public KmlFormat CreateKml(IEnumerable<SmsExtension> messages)
        {
            return CreateKml(messages, KmlFactorySettings.Default);
        }

        public KmlFormat CreateKml(IEnumerable<SmsExtension> messages, KmlFactorySettings settings)
        {
            KmlFormat KmlFormat = new KmlFormat();
            Style pathStyle = StylesUtil.ApplyPathStyle(KmlFormat);
            StylesUtil.ApplyPlacemarkTransparencyStyle(KmlFormat);

            Dictionary<string, Dictionary<LatLong, List<SmsExtension>>> messagesGroupBySenderNameAndLocation = SmsMessagesManager.GetMessageGroupBySenderNameAndLocation(messages);

            foreach (string senderName in messagesGroupBySenderNameAndLocation.Keys)
            {
                Folder folder = new Folder(senderName);
                KmlFormat.Folders.Add(folder);

                KmlPath path = new KmlPath(senderName);
                path.Style = pathStyle;

                foreach (LatLong location in messagesGroupBySenderNameAndLocation[senderName].Keys)
                {
                    path.Coordinates.Add(location);

                    Placemark placemark = CreatePlacemark(senderName, location, messagesGroupBySenderNameAndLocation[senderName][location], settings);

                    folder.Placemarks.Add(placemark);
                }

                // The latest placemark is the thread must be 100% opaque always
                folder.Placemarks[folder.Placemarks.Count - 1].StyleUrl = StylesUtil.MessageMinutesAgoCategory1Style;

                if (path.Coordinates.Count > 1)
                {
                    folder.Paths.Add(path);
                }
            }

            return KmlFormat;
        }
    }
}
